package com.weme.common.util

import android.app.Activity
import android.content.Context
import android.hardware.Camera
import android.hardware.Camera.getCameraInfo
import android.util.Log
import android.view.Surface
import java.lang.Math.abs

/**
     * 计算最完美的Size
     * @param sizes
     * @param expectWidth
     * @param expectHeight
     * @return
     */
    fun calculatePerfectSize(
        sizes: List<Camera.Size>, expectWidth: Int,
        expectHeight: Int, ratio: Float
    ): Camera.Size {
        var result = sizes[0]
        var widthOrHeight = false // 判断存在宽或高相等的Size
        // 辗转计算宽高最接近的值
        for (size in sizes) { // 如果宽高相等，则直接返回
            Log.e("tag",size.width.toString()+"/"+size.height)
            if (size.width == expectWidth && size.height == expectHeight) {
                result = size
                break
            }
            val w = (expectWidth * ratio).toInt()
            // 仅仅是宽度相等，计算高度最接近的size
            if (size.width == w) {
                widthOrHeight = true
                if (abs(result.height - expectHeight) > abs(size.height - expectHeight)) {
                    result = size
                }
            } else if (size.height == expectHeight) {
                widthOrHeight = true
                if (abs(result.width - w) > abs(size.width - w)) {
                    result = size
                }
            } else if (!widthOrHeight) {
                if (abs(result.width - w) > abs(size.width - w)
                    && abs(result.height - expectHeight) > abs(size.height - expectHeight)
                ) {
                    result = size
                }
            }
        }
        return result
    }

